package com.newvpn.fastbest.zenith;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.best.smart.center.CenterLog;
import com.github.shadowsocks.Core;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.newvpn.fastbest.zenith.activity.MainActivity;
import com.newvpn.fastbest.zenith.utils.AdSwith;
import com.newvpn.fastbest.zenith.utils.KeyStore;
import com.newvpn.fastbest.zenith.utils.ToastUtils;
import com.newvpn.fastbest.zenith.utils.VLog;

import java.io.File;

//                  ___                _
//                 / __|___  ___  __ _| |___
//                | (_ / _ \/ _ \/ _` |   -_)
//                 \___\___/\___/\__, |_\___|
//                               |___/
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \\|     |//  `.
//                     /  \\|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^O^^^^^^^^^^^
//                      佛主保佑，永不下架
public class VPNApp extends Application implements DefaultLifecycleObserver {

    private static  Context mContext;

//    private static boolean inApp = false;
//
//    public static boolean isInApp() {
//        return !isInBackground;
//    }

    private static boolean isInBackground = true;

//    public static void setInApp(boolean inApp) {
//        Log.e("setInApp",inApp+"");
//        VPNApp.inApp = inApp;
//    }

    public static Context getBase() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        com.github.shadowsocks.Core.app = this; // 初始化 app 属性
        //com.github.shadowsocks.Core.getCurrentProfile();
        Core.INSTANCE.initForJava(this, MainActivity.class);
        if (!isMainProcess()) {
            return;
        }
        File dir = new File(getApplicationInfo().nativeLibraryDir);

        CenterLog.init(getApplicationContext());
        ToastUtils.init(getApplicationContext());
        KeyStore.INS.init(getApplicationContext());
        VLog.init(getApplicationContext());
        initFirebase();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    private void initFirebase() {
        FirebaseMessaging.getInstance().setAutoInitEnabled(true);
        FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setMinimumFetchIntervalInSeconds(3601)
                .build();
        config.setConfigSettingsAsync(configSettings);
        config.fetchAndActivate()
                .addOnCompleteListener(new OnCompleteListener<Boolean>() {
                    @Override
                    public void onComplete(@NonNull Task<Boolean> task) {
                        if (task.isSuccessful()) {
                            AdSwith.hideAdAfterConneted = config.getBoolean("hideAdAfterConneted");
                            AdSwith.hideAdAfterDisConneted = config.getBoolean("hideAdAfterDisConneted");
                            AdSwith.hideAdAddTime = config.getBoolean("hideAdAddTime");
                            AdSwith.hideAdSplash = config.getBoolean("hideAdSplash");
                            AdSwith.hideAdAfterSeletVPN = config.getBoolean("hideAdAfterSeletVPN");
                            AdSwith.hideAdAfterSetApp = config.getBoolean("hideAdAfterSetApp");
                            AdSwith.hideAdAfterRefreshVPN = config.getBoolean("hideAdAfterRefreshVPN");
                        }
                    }
                });
    }



    private boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : activityManager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) {
                return getPackageName().equals(processInfo.processName);
            }
        }
        return false;
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Core.INSTANCE.updateNotificationChannels();
    }

    @Override
    public void onStart(@NonNull LifecycleOwner owner) {
        if (isInBackground) {
            // 应用从后台恢复到前台
            isInBackground = false;
            if (mShowLauchCallback != null){
                mShowLauchCallback.show();
            }
           // Log.d("MyApplication", "应用从后台恢复到前台");
        }
    }

    @Override
    public void onStop(@NonNull LifecycleOwner owner) {
        // 应用进入后台
        isInBackground = true;
        //Log.d("MyApplication", "应用进入后台");
    }

    static  ShowLauchCallback mShowLauchCallback;

    public static  void setShowLauchCallback(ShowLauchCallback callback){
        mShowLauchCallback =  callback;
    }


    public interface ShowLauchCallback{
        void show();
    }
}
